package com.shiro.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

public class RoleOrFilter extends AuthorizationFilter {

    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {

        Subject subject = getSubject(servletRequest, servletResponse);
        String[] roles = (String[])o;
        if (roles == null || roles.length == 0) {
            // 当前地址不用授权就可以访问
            return true;
        }
        for (String role : roles) {
            if (subject.hasRole(role)) {
                // 如果该主体拥有当前角色，就可以访问
                return true;
            }
        }
        return false;
    }
}
